package com.translation.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Properties;

import javax.servlet.http.Part;

public class Util {

	public static Properties loadProps(String propertyFile) {
		Properties props = null;
		InputStream is = null;
		try {
			is = Util.class.getResourceAsStream("/" + propertyFile);
			props = new Properties();
			props.load(is);
			return props;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return props;
	}

	public static String urlEncode(String str) {
		String ret = null;
		try {
			ret = URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		return ret;
	}

	public static String urlDecode(String name) {
		try {
			name = URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		return name;
	}

	public static Integer toInteger(String value, Integer defaultValue) {
		Integer i = toInteger(value);
		return (i != null) ? i : defaultValue;
	}

	public static Integer toInteger(String value) {
		Integer ret = null;
		if ((value = toNull(value)) != null) {
			try {
				ret = Integer.parseInt(value);
			} catch (NumberFormatException e) {
			}
		}
		return ret;
	}

	public static String toNull(String str) {
		if (str != null) {
			//str = str.trim();
			str = ("".equals(str)) ? null : str;
		}
		return str;
	}

	public static Double toDouble(String value) {
		Double ret = null;
		if ((value = toNull(value)) != null) {
			try {
				ret = Double.parseDouble(value);
			} catch (NumberFormatException e) {
			}
		}
		return ret;
	}

	public static boolean toBoolean(String value, boolean defaultValue) {
		return (value != null) ? Boolean.parseBoolean(value) : defaultValue;
	}

	public static String toLikeStr(String value) {
		return (value != null && !value.equals("")) ? '%' + value + '%' : null;
	}

	public static String sqlParams(int n) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n; i++) {
			if (i > 0) {
				sb.append(",");
			}
			sb.append("?");
		}
		return sb.toString();
	}

	public static String toEmpty(String string) {
		return (string == null) ? "" : string;
	}

	public static <T> T newInstance(String className, Class<T> type) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException {
		Class<?> klass = Class.forName(className);
		T obj = klass.asSubclass(type).newInstance();
		return obj;
	}

	public static String contents(File file) {
		StringBuilder sb = new StringBuilder();
		Reader r = null;
		try {
			try {
				r = new FileReader(file);
				int c = 1;
				while ((c = r.read()) != -1) {
					sb.append((char) c);
				}
			} finally {
				if (r != null) {
					r.close();
				}
			}
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
		return sb.toString();
	}

	public static String getFileName(Part file, String id) {
		String disposition = file.getHeader("content-disposition");
		for (String cd : disposition.split(";")) {
			if (cd.trim().startsWith("filename")) {
				return cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
			}
		}
		return "file" + "_" + id;
	}

}
